<?php
/**
 * HTTP request abstraction class
 *
 * @link http://www.django-fr.org/documentation/request_response/
 * @package dj
 * @subpackage http
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
class DjHttpRequest {

    /**
     * return current http request instance
     * @return DjHttpRequest
     */
    public static function current() {
        $path=self::removeBasePath($_SERVER['REQUEST_URI']);
        return new DjHttpRequest($path,$_SERVER['REQUEST_METHOD'],$_GET, $_POST);
    }

    /**
     * tells if key is present in post then get vars
     * @param string $key
     * @return bool
     */
    public function has_key($key) {

    }

    /**
     * return path and get vars string
     * @return string
     */
    public function get_full_path() {

    }

    /**
     * accessor for request vars, first post then get
     * @param string $key key to search
     * @return mixed
     */
    public function __get($key) {
        if (isset($this->POST[$key]))
            return $this->POST[$key];
        else if (isset($this->GET[$key]))
            return $this->GET[$key];
        else return null;
    }

    /**
     * constructor
     * @param string $path as /class/method/
     * @param string $method GET|POST
     * @param array $get get vars
     * @param array $post post vars
     */
    public function __construct($path,$method,$get=array(),$post=array()) {
        $this->path=$this->removeVarsFromUri($path);
        $this->method=$method;
        $this->GET=$get;
        $this->POST=$post;
    }/**/

    /**
     * @var string full request path excluding domain and vars
     */
    public $path;

    /**
     * @var string GET POST etc
     */
    public $method;

    /**
     * @var string
     */
    public $encoding;

    /**
     * @var array GET vars
     */
    public $GET;

    /**
     * @var array POST vars
     */
    public $POST;

    /**
     * @var Databean search in POST, then GET vars
     */
    // public $REQUEST;

    /**
     * @var array cookies vars
     */
    public $COOKIES;

    /**
     * remove physical path from document root from an url path
     * @param string $path
     * @return string path minus
     */
    protected function removeBasePath($path) {
        // defining base path to current application
        $base=dirname($_SERVER['PHP_SELF']); //echo $base;
        // removing base path if matching start of url
        if (substr($path,0,strlen($base))==$base) {
            $path=substr($path,strlen($base));
        }
        return $path;
    }

    /**
     * remove vars from request uri
     * @param string $uri
     * @return string
     */
    protected function removeVarsFromUri($uri) {
        if ($pos=strpos($uri,'?'))
            $uri=substr($uri,0,$pos);
        return $uri;
    }
}